import {
  getBusinessTable,
  editBusinessRecord,
  getAgentTable
} from '../../api/business'
import {
  BUSINESS_LIST,
  BUSINESS_CURRENT_ROW,
  BUSINESS_DATA_COUNT,
  BUSINESS_HISTORY_DATA,
  BUSINESS_PAGE_SIZE,
  AGENT_LIST
} from '../motations-type'

export default {
  state: {
    businessList: [],
    agentList: [],
    businessDataCount: null,
    businessPageSize: 20,
    businessHistoryData: [],
    row: {
      logintime: '',
      ctime: '',
      uname: '',
      password: '',
      tel: '',
      appid: '',
      tag: '',
      secret: '',
      auth: '',
      company: '',
      gid: '',
      trade: '',
      agentid: ''
    }
  },
  getters: {
    businessList: state => state.businessList
  },
  mutations: {
    [BUSINESS_LIST] (state, data) {
      state.businessList = data
    },
    [AGENT_LIST] (state, data) {
      state.agentList = data
    },
    [BUSINESS_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [BUSINESS_DATA_COUNT] (state, data) {
      state.businessDataCount = data
    },
    [BUSINESS_HISTORY_DATA] (state, data) {
      state.businessHistoryData = data
    },
    [BUSINESS_PAGE_SIZE] (state, data) {
      state.businessPageSize = data
    }
  },
  actions: {
    async getBusinessList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getBusinessTable(data)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          tid: item.tid,
          logintime: item.logintime,
          ctime: item.ctime,
          uname: item.uname,
          tel: item.tel.toString(),
          appid: item.appid,
          tag: item.tag,
          secret: item.secret,
          auth: item.auth.toString(),
          company: item.company,
          gid: item.gid,
          trade: item.trade,
          password: item.password,
          agentid: item.agentid.toString()
        }]
      })
      commit(BUSINESS_LIST, newData)
      commit(BUSINESS_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.business.businessPageSize) {
        commit(BUSINESS_HISTORY_DATA, newData)
      } else {
        commit(BUSINESS_HISTORY_DATA, newData.slice(0, rootState.business.businessPageSize))
      }
      return resp
    },
    async getAgentList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getAgentTable(data)
      resp.data.data.map((item) => {
        newData = [...newData, {
          id: item.id,
          uname: item.uname
        }]
      })
      commit(AGENT_LIST, newData)
      return resp
    },
    async editBusinessData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editBusinessRecord(data)
      return resp
    },
    async getBusinessCurrentRow ({ commit, rootState }, row) {
      commit(BUSINESS_CURRENT_ROW, row)
    },
    async changeBusinessPage ({ commit, rootState }, index) {
      let data = rootState.business.businessList
      let _start = (index - 1) * rootState.business.businessPageSize
      let _end = index * rootState.business.businessPageSize
      commit(BUSINESS_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeBusinessPageSize ({ commit, rootState }, value) {
      commit(BUSINESS_PAGE_SIZE, value)
    }
  }
}
